import csv
from SettingConfig import SettingConfig

class CsvToData:

    def __init__(self) -> None:
        self.values=dict()
        self.headerIndex=dict()

        self.isValuType=False
        pass
    
    def loadCsv(self,csvFilePath) ->None:
        with open (csvFilePath,"r") as f:
            csvF=csv.reader(f)
            csvHead=next(csvF)
            print(f"读到的列头：{csvHead}\n")
            print("构建列头下标\n")
            self._dealCsvHeadr(csvHead=csvHead)
            print("构建列头下标完成\n")
            for lineData in csvF:
                self._dealCsvLineDataPush(lineData)
                pass
               
            pass
        pass

    def _dealCsvHeadr(self,csvHead):
        csvCount=0      
        for cloumn in csvHead:#数据下标构建
            if cloumn == SettingConfig._defcsvInfo[0]:#变量类型区分
                self.headerIndex[SettingConfig._defcsvInfo[0]]=csvCount
                self.isValuType=True
            elif cloumn == SettingConfig._defcsvInfo[1]:#变量名
                self.headerIndex[SettingConfig._defcsvInfo[1]]=csvCount
            elif cloumn == SettingConfig._defcsvInfo[2]:#默认变量
                self.headerIndex[SettingConfig._defcsvInfo[2]]=csvCount
            elif cloumn == SettingConfig._defcsvInfo[3]:#变量描述
                self.headerIndex[SettingConfig._defcsvInfo[3]]=csvCount
            else:#其他方案           
                self.headerIndex[cloumn]=csvCount
            pass
            csvCount=csvCount+1
        csvCount=0
        pass

    def _dealCsvLineDataPush(self,csvData):
        keys=self.headerIndex.keys()
        if len(keys) != len(csvData):
            print("erro data check")
            return
        for key in keys:
            if key in self.values:
                pass
            else:
                self.values[key]=[]
                pass     
            self.values[key].append( csvData[ self.headerIndex[key] ] )
            pass 

        pass
  
    def clear(self) ->None:
        self.note=[]
        self.valueName=[]
        self.valueType=[]
        self.values=dict()
        pass

    pass